using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using C1.Win.C1FlexGrid;
using Arc.DairyMan.DairyMan.BusinessLogic;

namespace Arc.DairyMan.DairyMan.Common
{
    public partial class frmSearch_Eng : Form
    {
        #region Variables & Properties
        DataColumnCollection _Dc;
        ComboBox _Cmb_Bool;
        ComboBox _Cmb_String;
        ComboBox _Cmb_Other;
        string _SQL;
        public Press_Button PressedButton = Press_Button.None;

        ArrayList _Hidden_Field = new ArrayList();
        public ArrayList Hidden_Field
        {
            get { return _Hidden_Field; }
            set { _Hidden_Field = value; }
        }

        string _WhereClause = "";
        public string WhereClause
        {
            get { return _WhereClause; }
            set { _WhereClause = value; }
        }

        AbsBl _BC;
        public AbsBl BusinessClass
        {
            get { return _BC; }
            set
            {
                _BC = value;
                _BC.Search("1<>1");
                _Dc = _BC.DS.Tables[_BC.ModelObj.FriendlyName].Columns;

                Create_Combo();
                for (int i = 0; i < _Dc.Count; i++)
                {
                    //continue;
                    //MessageBox.Show(_Hidden_Field.IndexOf(_Dc[i]).ToString());
                    _Dc[i].Caption = _Dc[i].Caption.Replace("_", " ");
                    grdCriteria.Rows.Add();
                    grdCriteria[i + 1, 0] = _Dc[i].Caption;
                    grdCriteria[i + 1, 1] = "";
                    grdCriteria[i + 1, 2] = "";
                    if (_Hidden_Field.IndexOf(_Dc[i].ToString()) >= 0)
                        grdCriteria.Rows[i + 1].Visible = false;
                }
                //grdCriteria.Rows[1].Visible = false;
            }
        }

        bool _btnEdit = true;
        public bool BtnEdit
        {
            get { return _btnEdit; }
            set { _btnEdit = value; }
        }

        bool _btnDelete = true;
        public bool BtnDelete
        {
            get { return _btnDelete; }
            set { _btnDelete = value; }
        }

        bool _btnSelect = false;
        public bool BtnSelect
        {
            get { return _btnSelect; }
            set { _btnSelect = value; }
        }

        bool _Multi_Select = false;
        public bool Multi_Select
        {
            get { return _Multi_Select; }
            set { _Multi_Select = value; }
        }

        public Button EditButton { get { return btnEdit; } }
        #endregion

        #region Constructor & Form's Events
        public frmSearch_Eng()
        {
            InitializeComponent();
            tabMain.SelectTab(tabSearch);
        }

        private void frmSearch_Eng_Load(object sender, EventArgs e)
        {
            btnEdit.Enabled = BtnEdit;
            btnDelete.Enabled = BtnDelete;
            btnSelect.Enabled = BtnSelect;
        }
        #endregion

        #region Control's Events
        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                _SQL = "";
                for (int i = 1; i < grdCriteria.Rows.Count; i++)
                {
                    try
                    {
                        if (grdCriteria[i, "Condition"].ToString().Trim() != "")
                        {
                            if (grdCriteria[i, "Critearia_1"].ToString().Trim() == "" &&
                                !(_Dc[i - 1].DataType.Name.Trim() == "Boolean"))
                                continue;

                            if (_SQL.Length > 0)
                                _SQL += " and ";
                            string _ColumnName = grdCriteria[i, "Colunm"].ToString().Trim().Replace(' ', '_');
                            if (grdCriteria[i, "Condition"].ToString().Trim() == "Between")
                                _SQL += _ColumnName + " Between (" + grdCriteria[i, "Critearia_1"].ToString().Trim() + " and " + grdCriteria[i, "Critearia_2"].ToString().Trim();
                            else if (grdCriteria[i, "Condition"].ToString().Trim() == "Like")
                                _SQL += _ColumnName + " like " + (_Dc[i - 1].DataType.Name.Trim() == "String" ? "'%" : "%") + grdCriteria[i, "Critearia_1"].ToString().Trim() + (_Dc[i - 1].DataType.Name.Trim() == "String" ? "%'" : "%");
                            else if (grdCriteria[i, "Condition"].ToString().Trim() == "true")
                                _SQL += _ColumnName + " = 1";
                            else if (grdCriteria[i, "Condition"].ToString().Trim() == "false")
                                _SQL += _ColumnName + " = 0";
                            else if (grdCriteria[i, "Condition"].ToString().Trim() == "none")
                            {
                            }
                            else
                                _SQL += _ColumnName + " " + grdCriteria[i, "Condition"].ToString().Trim() + " " + (_Dc[i - 1].DataType.Name.Trim() == "String" ? "'" : "") + grdCriteria[i, "Critearia_1"].ToString().Trim() + (_Dc[i - 1].DataType.Name.Trim() == "String" ? "'" : "");
                        }
                    }
                    catch { }
                }

                if (_SQL.Trim().EndsWith("and"))
                    _SQL = _SQL.Substring(0, _SQL.Length - 4);

                if (_SQL != "" && _WhereClause != "")
                    _SQL += " and ";
                _SQL += _WhereClause;
                _BC.Search(_SQL);
                grdResult.DataSource = _BC.DS.Tables[_BC.ModelObj.FriendlyName].Copy();
                foreach (Column _dc in grdResult.Cols)
                    _dc.Caption = _dc.Caption.Replace("_", " ");
                //grdResult.Cols[0].Visible = false;
                foreach (string Field_Name in _Hidden_Field)
                    if (grdResult.Cols.Contains(Field_Name))
                        grdResult.Cols[Field_Name].Visible = false;
                tabMain.SelectedTab = tabResult;
                foreach (Column _dc in grdResult.Cols)
                    if (_dc.Visible)
                    {
                        grdResult.Col = _dc.Index;
                        break;
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void grdCriteria_RowColChange(object sender, EventArgs e)
        {
            switch (_Dc[grdCriteria.Row - 1].DataType.Name)
            {
                case "String":
                    grdCriteria.Cols[1].Editor = _Cmb_String;
                    break;
                case "Boolean":
                    grdCriteria.Cols[1].Editor = _Cmb_Bool;
                    break;
                default:
                    grdCriteria.Cols[1].Editor = _Cmb_Other;
                    break;
            }

            if (grdCriteria[grdCriteria.Row, 1] == null)
                return;

            switch (grdCriteria[grdCriteria.Row, 1].ToString())
            {
                case "=":
                case "<>":
                case "Like":
                case ">":
                case "<":
                case ">=":
                case "<=":
                    grdCriteria.Cols[2].AllowEditing = true;
                    grdCriteria[grdCriteria.Row, 3] = "";
                    break;
                case "Between":
                    grdCriteria.Cols[2].AllowEditing = true;
                    grdCriteria.Cols[3].AllowEditing = true;
                    break;
            }
        }

        private void grdResult_DoubleClick(object sender, EventArgs e)
        {
            if (btnEdit.Enabled)
                btnEdit_Click(sender, e);
            else if (btnSelect.Enabled)
                btnSelect_Click(sender, e);
        }

        private void grdResult_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (btnEdit.Enabled)
                    btnEdit_Click(sender, e);
                else if (btnSelect.Enabled)
                    btnSelect_Click(sender, e);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            PressedButton = Press_Button.Close;
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < grdCriteria.Rows.Count; i++)
            {
                grdCriteria[i, 1] = "";
                grdCriteria[i, 2] = "";
                grdCriteria[i, 3] = "";
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdResult.Row < 1)
                    return;
                _BC.ModelObj.PrimaryKey = grdResult[grdResult.Row, 0];
                _BC.Select(grdResult.Cols[0].Name + "='" + grdResult[grdResult.Row, 0] + @"'");
                _BC.ModelObj.TransactionMode = "U";
                _BC.ModelObj.PrimaryKey = grdResult[grdResult.Row, 0];
                PressedButton = Press_Button.Edit;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (grdResult.Row < 1)
                return;
            if (DialogResult.No == MessageBox.Show("Are you sure?", "Delete Record", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2))
                return;
            try
            {
                _BC.Select(grdResult.Cols[0].Name + "= '" + grdResult[grdResult.Row, 0] + "'");
                _BC.ModelObj.TransactionMode = "D";
                _BC.ModelObj.PrimaryKey = grdResult[grdResult.Row, 0];
                int _Result = _BC.Transaction(true);
                _BC.ModelObj.TransactionMode = "I";
                if (_Result > 0)
                    grdResult.RemoveItem(grdResult.Row);
                else
                    MessageBox.Show("Record is not deleted");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                _BC.ModelObj.TransactionMode = "I";
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdResult.Row < 1)
                    return;
                if (!Multi_Select)
                {
                    _BC.Select(grdResult.Cols[0].Name + "='" + grdResult[grdResult.Row, 0] + @"'");
                    _BC.ModelObj.PrimaryKey = grdResult[grdResult.Row, 0];
                }
                else
                    _BC.Select(_SQL);
                _BC.ModelObj.TransactionMode = "S";
                PressedButton = Press_Button.Select;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region Private Methods
        private void Create_Combo()
        {
            _Cmb_String = new ComboBox();
            _Cmb_String.Items.Add("=");
            _Cmb_String.Items.Add("<>");
            _Cmb_String.Items.Add("Like");

            _Cmb_Bool = new ComboBox();
            _Cmb_Bool.Items.Add("none");
            _Cmb_Bool.Items.Add("true");
            _Cmb_Bool.Items.Add("false");

            _Cmb_Other = new ComboBox();
            _Cmb_Other.Items.Add("=");
            _Cmb_Other.Items.Add("<>");
            _Cmb_Other.Items.Add(">");
            _Cmb_Other.Items.Add(">=");
            _Cmb_Other.Items.Add("<");
            _Cmb_Other.Items.Add("<=");
            _Cmb_Other.Items.Add("Like");
            _Cmb_Other.Items.Add("Between");
        }
        #endregion
    }
}